// 从本地存储加载待办事项
// 初始化待办列表（包含完成状态）
let todos = JSON.parse(localStorage.getItem('todos')) || [];
todos = todos.map(todo => typeof todo === 'string' ? { text: todo, isCompleted: false } : todo); // 兼容旧数据格式

// 渲染待办列表
function renderTodos() {
    const pendingList = document.getElementById('pendingList');
    const completedList = document.getElementById('completedList');
    todoList.innerHTML = '';
    todos.forEach((todo, index) => {
        const li = document.createElement('li');
        li.innerHTML = `
    <span class="${todo.isCompleted ? 'completed' : ''}">${todo.text}</span>
    <button class="complete-btn" onclick="toggleComplete(${index})">${todo.isCompleted ? '取消完成' : '完成'}</button>
    <button class="delete-btn" onclick="deletePermanently(${index})">彻底删除</button>
`;
        todoList.appendChild(li);
    });
}

// 添加待办事项
function addTodo() {
    const input = document.getElementById('todoInput');
    const todoText = input.value.trim();
    if (todoText) {
        todos.push(todoText);
        input.value = '';
        saveTodos();
        renderTodos();
    }
}

// 删除待办事项
// 切换完成状态
function toggleComplete(index) {
    todos[index].isCompleted = !todos[index].isCompleted;
    saveTodos();
    renderTodos();
}

// 彻底删除待办
function deletePermanently(index) {
    if (confirm('确认彻底删除该事项？')) {
        todos.splice(index, 1);
        saveTodos();
        renderTodos();
    }
}

// 保存到本地存储
function saveTodos() {
    localStorage.setItem('todos', JSON.stringify(todos));
}

// 页面加载时渲染
window.onload = () => {
    renderTodos();
    // 搜索输入监听
    document.getElementById('searchInput').addEventListener('input', searchTodos);
};

// 编辑待办事项
let editingIndex = -1;
function editTodo(index) {
    editingIndex = index;
    document.getElementById('todoInput').value = todos[index].text;
}

// 搜索待办事项
function searchTodos() {
    const keyword = document.getElementById('searchInput').value.toLowerCase();
    const filtered = todos.filter(todo => todo.text.toLowerCase().includes(keyword));
    renderTodos(filtered); // 修改渲染函数支持传入过滤数据
}

// 修改渲染函数支持过滤
function renderTodos(filteredTodos) {
    const pendingList = document.getElementById('pendingList');
const completedList = document.getElementById('completedList');
    pendingList.innerHTML = '';
      completedList.innerHTML = '';
      const displayTodos = filteredTodos || todos;
    displayTodos.forEach((todo, index) => {
        const li = document.createElement('li');
        if (todo.isCompleted) {
          li.classList.add('completed');
          completedList.appendChild(li);
        } else {
          pendingList.appendChild(li);
        }
        li.innerHTML = `
          <span>${todo.text}</span>
          <div class="button-group">
            <button class="edit-btn action-btn" onclick="editTodo(${index})"><img src="edit-icon.svg" alt="编辑"/></button>
            <button class="complete-btn action-btn" onclick="toggleComplete(${index})">${todo.isCompleted ? '取消完成' : '完成'}</button>
            <button class="delete-btn action-btn" onclick="deletePermanently(${index})"><img src="delete-icon.svg" alt="删除"/></button>
          </div>
        `;
      });
}

// 修改添加函数支持编辑
function addTodo() {
    const input = document.getElementById('todoInput');
    const todoText = input.value.trim();
    if (todoText) {
        if (editingIndex !== -1) {
            todos[editingIndex].text = todoText;
            editingIndex = -1;
        } else {
            todos.push({text: todoText, isCompleted: false});
        }
        input.value = '';
        saveTodos();
        renderTodos();
    }
}